<?php

namespace app\api\controller;

use app\common\controller\Logined;
use think\Db;

use app\common\service\BalanceService;
//提币
class Out extends Logined{


	/*
	获取提币列表
	*/
	public function getOutList(){
		$uid = $this->uid;
		if(empty($uid)){
			$this->retData(1,'缺少参数');
		}
		$data = model('OutConfigModel')->outList($uid);
		if($data){
			$this->retData(0,'success',$data);
		} else {
			$this->retData(0,'fail');
		}
	}	

	/*
	获取单个钱包提币
	 */
	public function getOutOne(){
		$uid = $this->uid;
		$wid = P('wid')?:1;
		if(empty($uid) || empty($wid)){
			$this->retData(1,'缺少参数');
		}
		$data = model('OutConfigModel')->outOne($uid,$wid);
		if($data){
			$this->retData(0,'success',$data);
		} else {
			$this->retData(0,'fail');
		}
	}


	/*
	提币
	*/
	public function Out(){
		if(C('out_status') != 1){
			$this->retData(1,'提币功能维护中~~');
		}
		$uid      = $this->uid;
		$id       = P('id')?:11;
		$amount   = P('amount');
		$address  = P('address')?:'0x';
		$code     = P('code')?:'00000';
		$paypass  = P('paypass');
		// $password = P('password');
		// $fee      = P('fee');
		$tag      = P('tag')?:'';
		if(empty($uid) || empty($id) || empty($amount) || empty($paypass) || empty($address) || empty($code)){
			$this->retData(1,'缺少参数');
		}
		if (strlen($address) != 34) {
			$this->retData(1,'地址有误');
		}
		$time = time();
        $h    = date('H',$time);
        $i    = date('i',$time);
        $i    = $h*60+$i;
        $start_time1 = C('out_start');
        $end_time1   = C('out_end');
        $start_time = explode(':', $start_time1);
        $end_time = explode(':', $end_time1);
        $start_time = $start_time[0]*60+$start_time[1];
        $end_time   = $end_time[0]*60+$end_time[1];
        if($start_time>$i || $end_time<$i){
            // $this->retData(1, '请在'.$start_time1.'-'.$end_time1.'提币');
        }
		if($amount <= 0){
			$this->retData(1,'提币数量有误，请重新输入');
		}

		$user_info = $this->user_info;
		if ($paypass != $user_info['paypass']) {
			$this->retData(1,'交易密码错误');
		}

		if ($this->user_info['out_status'] == 0) {
			$this->retData(1,'提币状态有误');
		}
		// $user_info = $this->user_info;
		// if (md5($password) != $user_info['password']) {
		// 	$this->retData(1,'密码错误');
		// }
		// if ($user_info['phone']) {
		// 	$veri_code = model('PhoneCodeModel')->getPhoneCode($user_info['phone'],1);
		// 	if ($code != $veri_code) {
		// 		$this->retData(1, '验证码错误或者已过期');
		// 	}
		// } else {
		// 	$veri_code = model('MailCodeModel')->getMailCode($user_info['mail'],1);
		// 	if ($code != $veri_code) {
		// 		$this->retData(1, '验证码错误或者已过期');
		// 	}
		// }
		
		//配置是否存在
		// $config = model('OutConfigModel')->getOneConfig($id,$uid);
		$config = Db::table('tp_out_config')->where('id',11)->find();
		$config['balance'] = Db::table('tp_user_balance')->where('uid',$uid)->where('wid',1)->value('balance');
		if(empty($config)){
			$this->retData(1,'未找到提币配置');
		}
		// //是否已绑定
		// $has = Db::table('tp_out_address')->where('uid',$uid)->find();
		// if (empty($has)) {
		// 	$this->retData(1,'请先设置收款地址');
		// } else {
		// 	$address = $has['address'];
		// }

		$underCheck = model('OutRecordModel')->underCheck($uid,$config['cid']);
		if($underCheck){
			$this->retData(1,'已存在待审核的订单！');
		}
		
		if($amount < $config['min_amount']){
			$this->retData(1,'最低提币数量:'.$config['min_amount']);
		}
		if($amount > $config['max_amount']){
			$this->retData(1,'最大提币数量:'.$config['max_amount']);
		}
		if ($config['balance'] < $amount) {
			$this->retData(1,'余额不足');
		}
		if (($config['balance'] * 0.4) < $amount) {
			$is_wu = 1;
		} else {
			$is_wu = 0;
		}
		if($config['fee_type'] == 1){
			$fee           = $amount*$config['fee'];
		} else {
			$fee           = $config['fee'];
		}
		$left_amount = $amount - $fee;
		if ($left_amount<=0) {
			$this->retData(1,'手续费不足');
		}
		$wid = $config['wid'];
		$cid = $config['cid'];

        Db::startTrans();
        $has = Db::table('tp_out_record')->where('uid',$uid)->where('cid',$cid)->whereTime('date','>',date('Y-m-d'))->count();
		if ($has >= $config['day_num']) {
			Db::rollback();
			$this->retData(1,'每日限'.$config['day_num'].'次提币，今日次数已用尽');
		}
		if (empty($tag)) {
			$tag = 'trc20';
		}
		//提币
		$res1 = model('OutRecordModel')->addRecord($uid,$wid,$cid,$amount,$fee,$address,$tag,$is_wu);
		$res2 = BalanceService::updateBalance($uid,$wid,-$amount,'提币(含手续费'.
			$fee.')',21,$res1,$address);

		//上链
		$res4 = $res5 = 1;
		if ($wid == 10) {
			
		}
		if($res1 && $res2 && $res4 && $res5){
			Db::commit();
			$this->retData(0,'提币申请成功，请等待！');
		} else {
			Db::rollback();
			$this->retData(1,'提币失败');
		}
	}


	/*
	提币记录
	 */
	public function getRecord(){
		$uid = $this->uid;
		if(empty($uid)){
			$this->retData(1,'缺少参数');
		}
		$data = model('OutRecordModel')->getRecord($uid);
		if($data){
			foreach ($data as $key => $val) {
				$data[$key]['amount'] = $val['amount']/1;
				$data[$key]['fee'] = $val['fee']/1;
				$data[$key]['left_amount'] = $val['left_amount']/1;
			}
			$this->retData(0,'success',$data);
		} else {
			$this->retData(0,'fail');
		}
	}

	/*
	绑定提币地址
	 */
	// public function bindAddress(){
	// 	$uid = $this->uid;
	// 	$address = P('address');
	// 	if(empty($uid) || empty($address)){
	// 		$this->retData(1,'缺少参数');
	// 	}
	// 	if (substr($address, 0,2) != '0x' || strlen($address) != '42') {
	// 		$this->retData(1,'地址有误');
	// 	}
	// 	//是否已绑定
	// 	$has = Db::table('tp_out_address')->where('uid',$uid)->find();
	// 	if ($has) {
	// 		$this->retData(1,'已绑定地址');
	// 	}
	// 	$num = Db::table('tp_out_address')->where('address',$address)->count();
	// 	if ($num >= 3) {
	// 		$this->retData(1, '一个地址最多只能绑定三个账号');
	// 	}
	// 	$res = Db::table('tp_out_address')->insert([
	// 		'uid'=>$uid,
	// 		'address'=>$address,
	// 	]);
	// 	if ($res) {
	// 		$this->retData(0, '操作成功');
	// 	} else {
	// 		$this->retData(1, '操作失败');
	// 	}
	// }

	// /*
	// 是否绑定地址
	//  */
	// public function isAddress(){
	// 	$uid = $this->uid;
	// 	if(empty($uid)){
	// 		$this->retData(1,'缺少参数');
	// 	}
	// 	//是否已绑定
	// 	$has = Db::table('tp_out_address')->where('uid',$uid)->find();
	// 	if ($has) {
	// 		$this->retData(0,'已绑定地址',$has['address']);
	// 	} else {
	// 		$this->retData(0,'未绑定地址','');
	// 	}
	// }


	//地址列表
    public function dzList(){
        $uid = $this->uid;
        if (empty($uid)) {
            $this->retData(1, '缺少参数');
        }
        $data = Db::table('tp_user_wallet_list')->where('uid',$uid)->order('id desc')->select();
        $this->retData(0,'success',$data);
    }


    //地址新增
    public function dzAdd(){
        $uid = $this->uid;
        $id  = P('id')?:0;
        $name = P('name')?:'名称';
        $address  = P('address');
        $remark    = P('remark')?:'无';
        if (empty($uid) || empty($name) || empty($address)) {
            $this->retData(1, '缺少参数');
        }
        if ($id) {
        	$res = Db::table('tp_user_wallet_list')->where('uid',$uid)->where('id',$id)->update([
	            'uid'=>$uid,
	            'name'=>$name,
	            'address'=>$address,
	            'remark' =>$remark,
	            'date'=>date('Y-m-d H:i:s')
	        ]);
	        if ($res) {
	            $this->retData(0, '修改成功');
	        } else {
	            $this->retData(1, '修改失败');
	        }
        } else {
        	$res = Db::table('tp_user_wallet_list')->insert([
	            'uid'=>$uid,
	            'name'=>$name,
	            'address'=>$address,
	            'remark' =>$remark,
	            'date'=>date('Y-m-d H:i:s')
	        ]);
	        if ($res) {
	            $this->retData(0, '新增成功');
	        } else {
	            $this->retData(1, '新增失败');
	        }
        } 
    }

    //地址删除
    public function dzDel(){
        $uid = $this->uid;
        $id = P('id');
        if (empty($uid) || empty($id)) {
            $this->retData(1, '缺少参数');
        }
        $dz = Db::table('tp_user_wallet_list')->where('uid',$uid)->where('id',$id)->find();
        if (empty($dz)) {
            $this->retData(1, 'system error');
        }
        $res = Db::table('tp_user_wallet_list')->where('id',$id)->delete();
        if ($res) {
            $this->retData(0, '删除成功');
        } else {
            $this->retData(1, '删除失败');
        }
    }
}